package com.bjpowernode.handlers;

import cn.hutool.json.JSONUtil;
import com.bjpowernode.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AppAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        Result result = Result.builder().code(401).msg("没有访问权限").build();
        String json = JSONUtil.toJsonStr(result);
        response.getWriter().write(json);
    }
}
